package com.gzz.orchid.main.console.controller.rbac;

import com.gzz.orchid.common.core.model.ActionCmd;
import com.gzz.orchid.common.core.tuple.TreeNode;
import com.gzz.orchid.common.webmvc.support.HttpResult;
import com.gzz.orchid.context.application.rbac.IModuleExecutor;
import com.gzz.orchid.context.application.rbac.IModuleHandler;
import com.gzz.orchid.context.application.rbac.command.ModuleSaveCmd;
import com.gzz.orchid.context.application.rbac.pojo.ModuleDto;
import com.gzz.orchid.context.application.rbac.pojo.ModuleVo;
import com.gzz.orchid.context.application.rbac.queries.ModuleQuery;
import com.gzz.orchid.context.infrastructure.definition.Action;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模块管理
 *
 * @author higmd
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/rbac/module")
@RequiredArgsConstructor
public class ModuleController {
    private final IModuleExecutor executor;
    private final IModuleHandler handler;

    /**
     * 获取模块操作
     */
    @RequestMapping("/actions")
    public HttpResult actions() {
        log.info("获取模块操作权限");
        return HttpResult.success()
                .data(Action.values());
    }

    /**
     * 获取模块树
     */
    @RequestMapping("/options")
    public HttpResult options() {
        log.info("获取模块树");
        List<TreeNode<Long>> treeList = handler.options();
        return HttpResult.success()
                .data(treeList);
    }

    /**
     * 列表
     */
    @RequestMapping("/listByPage")
    public HttpResult listByPager(ModuleQuery query) {
        log.info("列表:{}", query);
        List<ModuleDto> pageData = handler.listByPager(query);
        return HttpResult.success()
                .data(pageData)
                .pager(query.getPager());
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public HttpResult list(ModuleQuery query) {
        log.info("列表:{}", query);
        List<ModuleDto> listData = handler.list(query);
        return HttpResult.success()
                .data(listData);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public HttpResult detail(@NotNull(message = "required.parameter") Long id) {
        log.info("详情: {}", id);
        ModuleVo detail = handler.detail(id);
        return HttpResult.success()
                .data(detail);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public HttpResult save(@RequestBody @Valid ModuleSaveCmd saveCmd) {
        log.info("保存:{}", saveCmd);
        executor.save(saveCmd);
        return HttpResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public HttpResult delete(@Valid ActionCmd cmd) {
        log.info("删除:{}", cmd);
        executor.delete(cmd);
        return HttpResult.success();
    }

    /**
     * 禁用
     */
    @RequestMapping("/disable")
    public HttpResult disable(@Valid ActionCmd cmd) {
        log.info("禁用:{}", cmd);
        executor.disable(cmd);
        return HttpResult.success();
    }

    /**
     * 启用
     */
    @RequestMapping("/enable")
    public HttpResult enable(@Valid ActionCmd cmd) {
        log.info("启用:{}", cmd);
        executor.enable(cmd);
        return HttpResult.success();
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    public HttpResult audit(@Valid ActionCmd cmd) {
        log.info("审核:{}", cmd);
        executor.audit(cmd);
        return HttpResult.success();
    }
}
